﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AvatarHelper;
using Avatar.Model;
using System.Web;
using System.Web.Security;
using Avatar.Common;
using Avatar.ResumeService;
using Avatar.Company;

namespace Avatar.MembershipService
{
    public static class LoginManager
    {
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="_name"></param>
        /// <param name="_pwd"></param>
        /// <param name="_vCode"></param>
        /// <param name="_groupID"></param>
        /// <returns></returns>
        public static void Login(string _email, string _pwd, string _vCode,bool _rememberMe)
        {
            #region 入口检测
            if (string.IsNullOrEmpty(_email)) throw new Exception("用户名不能为空");
            if (string.IsNullOrEmpty(_pwd)) throw new Exception("密码不能为空");
            if (string.IsNullOrEmpty(_vCode)) throw new Exception("验证码不能为空");
            #endregion

            #region 验证码
            string _code = HttpContext.Current.Session["ImgCode"] == null ? string.Empty : HttpContext.Current.Session["ImgCode"].ToString();
            if (_code != _vCode) throw new Exception("验证码错误！");
            #endregion

            if (Membership.ValidateUser(_email, _pwd))
            {
                FormsAuthentication.RedirectFromLoginPage(_email, _rememberMe);
            }
            else
            {
                throw new Exception("用户名或密码错误！");
            }
        }

        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="_pwd"></param>
        /// <param name="_email"></param>
        /// <param name="_vCode"></param>
        /// <param name="_groupID"></param>
        /// <returns></returns>
        public static void Register(string _pwd, string _email, string _vCode, string _roleName)
        {
            #region 入口检测
            if (string.IsNullOrEmpty(_pwd)) throw new Exception("密码不能为空");
            if (string.IsNullOrEmpty(_email)) throw new Exception("Email不能为空");
            if (string.IsNullOrEmpty(_vCode)) throw new Exception("验证码不能为空");
            if (string.IsNullOrEmpty(_roleName)) throw new Exception("用户组名称不能为空");
            #endregion

            #region 验证码
            string _code = HttpContext.Current.Session["ImgCode"] == null ? string.Empty : HttpContext.Current.Session["ImgCode"].ToString();
            if (_code != _vCode) throw new Exception("验证码错误！");
            #endregion

            Membership.CreateUser(_email, _pwd, _email);

            Roles.AddUserToRole(_email, _roleName);
            if (_roleName == "个人用户")
            ResumeManager.Initialization(_email);
            if (_roleName == "企业用户") CompanyServiceManager.Initialization(_email);
        }
        public static void Register(string _email, string _roleName)
        {
            #region 入口检测
            if (string.IsNullOrEmpty(_email)) throw new Exception("Email不能为空");
            if (string.IsNullOrEmpty(_roleName)) throw new Exception("用户组名称不能为空");
            #endregion

            var _pwd = Membership.GeneratePassword(6, 2);
            Membership.CreateUser(_email, _pwd, _email);

            Roles.AddUserToRole(_email, _roleName);
            if (_roleName == "个人用户")
                ResumeManager.Initialization(_email);
            if (_roleName == "企业用户") CompanyServiceManager.Initialization(_email);
        }
        /// <summary>
        /// 系统退出并转向网站首页
        /// </summary>
        public static void Logout()
        {
            FormsAuthentication.SignOut();
            HttpContext.Current.Response.Redirect("~/");
        }

        /// <summary>
        /// 限制除个人用户以外禁止访问
        /// </summary>
        /// <param name="_returnURL"></param>
        public static void PersonAccessCheck(string _returnURL)
        {
            if (Membership.GetUser() == null)
            {
                Helper.ShowMassage("此内容需登录后才可访问.", "./login.aspx?returnurl=" + _returnURL);
                //FormsAuthentication.RedirectToLoginPage();
            }
            else if (!Roles.IsUserInRole("个人用户"))
            {
                Helper.ShowMassage("您的访问权限不够,请以其他身份用户登录或使用其他功能.", "./login.aspx?returnurl=" + _returnURL);
                //FormsAuthentication.RedirectToLoginPage();
            }
        }
        /// <summary>
        /// 限制除管理员用户以外禁止访问
        /// </summary>
        public static void AdministratorAccessCheck()
        {
            CompanyAccessCheck(HttpContext.Current.Request.Url.ToString());
        }
        public static void AdministratorAccessCheck(string _returnURL)
        {
            if (Membership.GetUser() == null)
            {
                Helper.ShowMassage("此内容需登录后才可访问.", "./login.aspx?returnurl=" + _returnURL);
                //FormsAuthentication.RedirectToLoginPage();
            }
            else if (!Roles.IsUserInRole("系统管理员"))
            {
                Helper.ShowMassage("您的访问权限不够,请以其他身份用户登录或使用其他功能.", "./login.aspx?returnurl=" + _returnURL);
                //FormsAuthentication.RedirectToLoginPage();
            }
        }

        /// <summary>
        /// 限制除个人用户以外禁止访问
        /// </summary>
        public static void PersonAccessCheck()
        {
            PersonAccessCheck(HttpContext.Current.Request.Url.ToString());
        }
        /// <summary>
        /// 限制除企业用户以外禁止访问
        /// </summary>
        public static void CompanyAccessCheck()
        {
            CompanyAccessCheck(HttpContext.Current.Request.Url.ToString());
        }
        /// <summary>
        /// 限制除企业用户以外禁止访问
        /// </summary>
        /// <param name="_returnURL"></param>
        public static void CompanyAccessCheck(string _returnURL)
        {
            if (Membership.GetUser() == null)
            {
                Helper.ShowMassage("此内容需登录后才可访问.", "./login.aspx?returnurl=" + _returnURL);
                //FormsAuthentication.RedirectToLoginPage();
            }
            else if (!Roles.IsUserInRole("企业用户"))
            {
                Helper.ShowMassage("您的访问权限不够,请以其他身份用户登录或使用其他功能.", "./login.aspx?returnurl=" + _returnURL);
                //FormsAuthentication.RedirectToLoginPage();
            }
        }
        /// <summary>
        /// 获取当前用户对象
        /// </summary>
        /// <returns></returns>
        public static MembershipUser GetCurrentUser()
        {
            if (Membership.GetUser() == null) Helper.ShowMassage("登录超时，请重新登录系统.", "./login.aspx?returnurl=" + HttpContext.Current.Request.Url.ToString());
            return Membership.GetUser();
        }
    }
}
